import axios from "axios";
let host = "";

axios.defaults.timeout = 50000;

// let url = window.location.href.split('#')[0] + 'api'
// axios.defaults.baseURL = url



axios.interceptors.request.use(

    config => {

        return config;
    },
    error => {
        return Promise.reject(error);
    }
);


axios.interceptors.response.use(response => {

    return response
}, err => {


    return Promise.resolve(err.response)
})


/**
 * 
 * @param url
 * @param data
 * @returns {Promise}
 */

export function get(url, params = {}) {

    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: params
        })
            .then(response => {
                resolve(response);
            })
            .catch(err => {
                reject(err)
            })
    })
}


/**
 * 
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post(url, data = {}) {

    return new Promise((resolve, reject) => {
        axios.post(url, data)
            .then(response => {
                resolve(response);
            }, err => {
                reject(err)
            })
    })
}

export function postcontent(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.post(url, data, {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8"'
            },
        })
            .then(response => {
                resolve(response);
            }, err => {
                reject(err)
            })
    })
}


/**
 * 
 * @param url
 * @param data
 * @returns {Promise}
 */

export function patch(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.patch(url, data)
            .then(response => {
                resolve(response);
            }, err => {
                reject(err)
            })
    })
}

/**
 * 
 * @param url
 * @param data
 * @returns {Promise}
 */

export function put(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.put(url, data)
            .then(response => {
                resolve(response);
            }, err => {
                reject(err)
            })
    })
}
